package com.project2.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.bo.CommentListBO;
import com.project2.bean.po.MarketComment;
import com.project2.bean.po.MarketCommentExample;
import com.project2.bean.po.MarketUser;
import com.project2.bean.vo.CommentCountVO;
import com.project2.bean.vo.CommentListVO;
import com.project2.bean.vo.CommentUserInfoVO;
import com.project2.mapper.MarketCommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : bunnyupup
 * @description :
 * @date : Created in 2022/8/15 17:43
 */
@Service
public class WxCommentServiceImpl implements WxCommentService {
    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Override
    public CommonData<CommentListVO> list(CommentListBO bo) {
        // 分页
        PageHelper.startPage(bo.getPage(), bo.getLimit());
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        if(bo.getShowType()==1){
            criteria.andHasPictureEqualTo(true);
        }
        Integer valueId = bo.getValueId();
        criteria.andValueIdEqualTo(valueId);
//        criteria.andValueIdEqualTo(bo.getValueId());
        criteria.andTypeEqualTo(bo.getType());
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(example);

//        List<MarketComment> marketComments = marketCommentMapper.selectByCondition(bo);


        PageInfo<MarketComment> pageInfo = new PageInfo<>(marketComments);

        List<CommentListVO> commentListVOS = new ArrayList<>();
        for (MarketComment marketComment : marketComments) {
            CommentListVO vo = new CommentListVO();
            vo.setAddTime(marketComment.getAddTime());
            vo.setAdminContent(marketComment.getContent());
            vo.setContent(marketComment.getContent());
            vo.setPicList(marketComment.getPicUrls());
            CommentUserInfoVO commentUserInfoVO = marketCommentMapper.selectUserInfo(marketComment.getUserId());
            commentUserInfoVO.setAvatarUrl("");
            vo.setUserInfo(commentUserInfoVO);
            commentListVOS.add(vo);
        }
        CommonData data = CommonData.data(pageInfo);
        data.setList(commentListVOS);


        return data;
    }

    @Override
    public CommentCountVO count(Integer valueId, Byte type) {
//        Integer allCount = marketCommentMapper.countAllCommentByValueIdAndType(valueId, type);
//        Integer hasPicCount = marketCommentMapper.countHasPicCommentByValueIdAndType(valueId, type);
//        CommentCountVO commentCountVO = new CommentCountVO();
//        commentCountVO.setAllCount(allCount);
//        commentCountVO.setHasPicCount(hasPicCount);
//        return commentCountVO;

        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(example);
        CommentCountVO commentCountVO = new CommentCountVO();
        commentCountVO.setAllCount(marketComments.size());

        criteria.andHasPictureEqualTo(true);
        List<MarketComment> marketComments1 = marketCommentMapper.selectByExample(example);
        commentCountVO.setHasPicCount(marketComments1.size());
        return commentCountVO;
    }

    @Override
    public MarketComment addTopicComment(MarketComment marketComment, MarketUser marketUser) {
        Integer id = marketUser.getId();
        marketComment.setUserId(id);

        Date date = new Date();
        marketComment.setAddTime(date);
        marketComment.setUpdateTime(date);

        int i = marketCommentMapper.insertSelective(marketComment);
        return marketComment;
    }
}
